package com.chenwenxuan.sync.ui;

import javax.swing.SwingUtilities;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.aspectj.lang.JoinPoint;

public class SwingInvokerLaterInterceptor implements MethodInterceptor {

	private static final class InvokeLaterTask implements Runnable {
		
		private MethodInvocation m;

		private InvokeLaterTask(MethodInvocation m) {
			this.m = m;
		}

		@Override
		public void run() {
			try {
				m.proceed();
			} catch (Throwable e) {
				e.printStackTrace();
			}
		}

	}
	
	public void around(JoinPoint joinPoint) {

	}

	@Override
	public Object invoke(MethodInvocation method) throws Throwable {
		if ( method.getMethod().getName().startsWith("ui")) {
			InvokeLaterTask task = new InvokeLaterTask(method);
			SwingUtilities.invokeLater(task);
		}
		return method.proceed();
	}


	
}
